#include<iostream>
#include<cstring>
#include<string>

using namespace std;

const int N = 26;

int n, m;
bool g[N][N], d[N][N];
bool st[N];

void floyed(){
    memcpy(g, d, sizeof d);

    for (int k = 0; k < n; k ++){
        for (int i = 0; i < n; i ++){
            for(int j = 0; j < n; j ++){
                g[i][j] |= (g[i][k] && g[k][j]);
            }
        }
    }
}

int check(){
    for (int i = 0; i < n; i ++){
        if(g[i][i]){
            return 0;
        }
    }

    for (int i = 0; i < n; i ++){
        for (int j = 0; j < i; j ++){
            if(!g[i][j] && !g[j][i]){
                return 1;
            }
        }
    }

    return 2;
}

char get_min(){
    for (int i = 0; i < n; i ++){
        bool find = true;
        if(!st[i]){
            for (int j = 0; j < n; j ++){
                if(!st[j] && g[j][i]){
                    find = false;
                    break;
                }
            }

            if(find){
                return i + 'A';
            }
        }
    }

    return 0;
}

int main(){

    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    while(cin >> n >> m, n || m){
        memset(d, 0, sizeof d);
        memset(st, 0, sizeof st);
        string temp;
        int type = 2, t;
        for (int i = 0; i < m; i ++){
            cin >> temp;
            int a = temp[0] - 'A', b = temp[2] - 'A';
            d[a][b] = 1;

            if(type){
                floyed();
                type = check();
                if(type)
                    t = i + 1;
            }
        }

        if(type == 0){
            printf("Inconsistency found after %d relations.\n", t);
        }else if(type == 1){
            printf("Sorted sequence cannot be determined.\n");
        }else{
            printf("Sorted sequence determined after %d relations: ", t);

            for (int i = 0; i < n; i ++){
                char c = get_min();
                st[c - 'A'] = true;
                printf("%c", c);
            }
            printf(".\n");
        }
    }
}